home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14951 < prev    next >
Encoding:
Text File  |  1996-08-05  |  6.7 KB  |  146 lines

  1. Path: news.cais.com!news
  2. From: Eric Vought <adfh@ids2.idsonline.com>
  3. Newsgroups: comp.object,comp.lang.eiffel,comp.lang.c++,comp.lang.beta,comp.lang.java,comp.lang.sather
  4. Subject: Re: What Should An Exception Handling Do? -- Clarification of rules
  5. Date: Tue, 02 Apr 1996 16:03:27 -0500
  6. Organization: Capital Area Internet Service info@cais.com 703-448-4470
  7. Message-ID: <3161961F.1FEE8722@ids2.idsonline.com>
  8. References: <4irn11$7ln@mimas.brunel.ac.uk> <Pine.Sola.3.91.960322041345.17711C-100000@ux5.cso.uiuc.edu> <4j03p4$fbt@hoho.quake.net> <Doq3sv.MzA@research.att.com> <4jcf89$2k9@crc-news.doc.ca> <4jdv0p$lnr@dscomsa.desy.de> <wpp.828370477@marie.physik.TU-Berlin.DE>
  9. NNTP-Posting-Host: ip158.idsonline.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01 (X11; I; Linux 1.2.13 i486)
  14.  
  15. > I don't know, though, if the above is a real-world example.  Most
  16. > people don't seem to program that way.  But it is an example, of
  17. > what *I* like to be able to do.  It requires:
  18. > * The exception handler must have both access to the context of the
  19. >   caller as well to that of the callee.  In particular, the callee's
  20. >   data must still exist, when the exception handler is reached.
  21. > * The exception handler must be able to decide, where to resume the
  22. >   execution of the callee, whether at the throw point, at some
  23. >   designated breakpoint, or not at all.
  24. > I don't see, how C++'s throw/catch/try scheme is able to handle this
  25. > general case.  This is one of the reasons, why I dislike C++, and prefer
  26. > BETA.  But this is a question of taste, not a question of reason.
  27.  
  28. Maybe I'm misunderstanding you, but I see several problems with this
  29. line of thought. First of all, the point of exception handling as I see
  30. it:
  31.  
  32. Exception handling is a way that the author of library routines that are
  33. used across many applications can defer decisions on how to handle
  34. errors to the caller of the routine. For instance, if a file cannot be
  35. accessed, rather than aborting or printing an error message to standard
  36. error, or automatically trying another file, the application designer,
  37. who has knowledge about what the system is trying to do and what
  38. recourses are available can catch the exception and attempt to deal with
  39. it. This may entail informaing the user, modifying the parameters of the
  40. mthod call and retrying, or other possibilities.
  41.  
  42. In any case, there are several things to be considered here:
  43.  
  44. 1) The library author has no knowledge about implementations that may
  45. use the library routine(s). Therefore, the library author cannot
  46. anticipate how the exception might be dealt with outside of the library
  47. routines themselves.
  48.  
  49. 2) The implementor should not have any knowledge about the
  50. *implementation* of the library routines. Even if the implementor has
  51. such information, using it can be disasterous in the case of changes in
  52. the library implementation over time.
  53.  
  54. 3) Exceptions flag *exceptional* circumstances. That is, circumstances
  55. that are not easily anticipated and do not occur often.
  56.  
  57. Item one seems to cause problems with resumption semantics because the
  58. library has no idea what a user might want to do in a error handling
  59. routine, and therefore has no idea if the state of the object is still
  60. consistent when the handler returns to the point of the throw. In other
  61. words, lets say that an object invariant is modified in the course of a
  62. method call. An exception is thrown, not terminating the method, and not
  63. executing a finally block or equivalent. That means that the object is
  64. in an inconsistent state when the error code executes and may or may not
  65. be (depending on what further methods are called) when the method
  66. resumes.
  67.  
  68. As a comparison, termination semantics allow the method to clean up,
  69. restoring invariantes before the error code executes. When the code is
  70. reentered, through a retry of the method call, the code can then be
  71. assured that it is in a known and consistent state.
  72.  
  73. Item two means that the error code cannot know about the implementation
  74. of the method, and therefore cannot know about what invariantes may or
  75. may not be temporarily violated within its context. Even if it did,
  76. unless the code were able to be run with private access to the object (a
  77. *very* bad idea), it could not make the object consistent before
  78. continuing. Even worse, if the implementation of the method were ever to
  79. change, all error code that took its implementation into account or
  80. accessed its state would break.
  81.  
  82. Item three means that common or easily anticipated conditions should not
  83. use exception handling. For one thing, exception handling is slow when
  84. compared to a method call or inlined code. Something like a CD skipping
  85. (and thus retrieving the next pice from a buffer) should not be handled
  86. by exceptions for two reasons: 1) it is a very common occurence (in a
  87. portable CD-Player anyway) and therefore should be optimized, and 2) it
  88. can be resolved in the context of the method and does not require the
  89. execution path to change. A simple if statement based on a method return
  90. value will suffice here.
  91.  
  92.  
  93. IMHO, situations that require resumption symantics can be handled much
  94. more cleanly in other ways, without violating OOP principles of
  95. encapsulations. If the library designer anticipates that a user would
  96. want to supply a error recovery routine in a resumption situation, then
  97. a registered callback or interfaced object can be supplied:
  98.  
  99. interface SpecialAlternateBuffer
  100. {
  101.     //...
  102. } // SpecialAlternateBuffer
  103.  
  104. class A
  105. {
  106.     SpecialAlternateBuffer sab;
  107.  
  108.     A(SpecialAlternateBuffer sab)
  109.     {
  110.         // initialize ...
  111.     } // A
  112.  
  113.     String TrySomethingThatWontWork(String location)
  114.     {
  115.         String returnVal = RetrieveFrom(location);
  116.         if (returnVal = null)
  117.         {
  118.             return sab.GetFromElsewhere(interestingData);
  119.         } else {
  120.             return returnVal();
  121.         }
  122.     } // TrySomethingThatWontWork
  123.  
  124.     //...
  125. } // A
  126.  
  127. This is a simplistic example, but it illustrates that an error is
  128. handled from the point where on might be tempted to throw an exception
  129. and all data that the error handling code has access to is explicitly
  130. given to it. Neither class A, nor the recovery code needs any
  131. information that it shouldn't have access to. Because the recovery code
  132. is called explicitly from a particular point, the method writer can
  133. ensure that the object is in a consistent state before the call.
  134. Obviouslt, in situations where the library author has even less of an
  135. idea of how the error might be handled, an exception should be thrown,
  136. providing a finally block for cleanup, and the method should be
  137. reentered after having fixed the location or specifying an alternative
  138. location.
  139.  
  140. Personally, I dislike the fact that Java does not allow method
  141. references, as they would be ideal in this situation. How hard would it
  142. be for them to implement references to static methods?
  143.  
  144.